package com.app.util.db.bean;

import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import com.app.util.db.DbUtilException;


public class ResultMap extends LinkedHashMap<String, Object> {

	private static final long serialVersionUID = 3644769971764690148L;

	private boolean noError = false;

	public boolean isNoError() {
		return noError;
	}

	public void setNoError(boolean noError) {
		this.noError = noError;
	}

	public Object get(Object key) {
		if (!containsKey(key)) {
			if (noError) {
				return null;
			} else {
				throw new DbUtilException("No field \"" + key + "\" in result.");
			}
		} 
		return super.get(key);
	}

	public String getString(String key) {
		return (String) get(key);
	}

	public String parseString(String key) {
		if (null == get(key)) {
			return null;
		}
		return get(key).toString();
	}

	public Character getChar(String key) {
		return (Character) get(key);
	}

	public Date getDate(String key) {
		return (Date) get(key);
	}

	public Float getFloat(String key) {
		return (Float) get(key);
	}

	public Float parseFloat(String key) {
		if (null == get(key)) {
			return null;
		}
		return Float.parseFloat(get(key).toString());
	}

	public Double getDouble(String key) {
		return (Double) get(key);
	}

	public Double parseDouble(String key) {
		if (null == get(key)) {
			return null;
		}
		return Double.parseDouble(get(key).toString());
	}

	public Boolean getBoolean(String key) {
		return (Boolean) get(key);
	}

	public Byte getByte(String key) {
		return (Byte) get(key);
	}

	public Byte parseByte(String key) {
		if (null == get(key)) {
			return null;
		}
		return Byte.parseByte(get(key).toString());
	}

	public Short getShort(String key) {
		return (Short) get(key);
	}

	public Short parseShort(String key) {
		if (null == get(key)) {
			return null;
		}
		return Short.parseShort(get(key).toString());
	}

	public Integer getInteger(String key) {
		return (Integer) get(key);
	}

	public Integer parseInteger(String key) {
		if (null == get(key)) {
			return null;
		}
		return Integer.parseInt(get(key).toString());
	}

	public Long getLong(String key) {
		return (Long) get(key);
	}

	public Long parseLong(String key) {
		if (null == get(key)) {
			return null;
		}
		return Long.parseLong(get(key).toString());
	}

	public BigDecimal getBigDecimal(String key) {
		return (BigDecimal) get(key);
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		boolean first = true;
		for (Map.Entry<String, Object> ent : entrySet()) {
			if (first) {
				first = false;
			} else {
				sb.append(", ");
			}
			sb.append(ent.getKey()).append("='").append(ent.getValue()).append("'");
		}
		return super.toString();
	}
}
